﻿#include "centerwgt.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QMessageBox>
#include <QFileInfo>
Q_DECLARE_METATYPE(AVFramePtr)
centerWgt::centerWgt(QWidget *parent)
    : QWidget(parent)
    ,player_(nullptr)
{
    this->setFixedSize(1080,600);
    this->setAcceptDrops(true);
    this->setStyleSheet("background-color:#1b1a20");
    this->setObjectName("centerWgt");
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_StyledBackground);
    InitPlayer();
}

bool centerWgt::Puase(const bool pause)
{
    if(!player_)
    {
        return false;
    }
    return player_->Puase(pause);
}

bool centerWgt::Volum(const qreal &v)
{
    if(!player_)
    {
        return false;
    }
    return player_->Volum(v);
}

void centerWgt::InitPlayer()
{
    player_.reset(new AVPlayer());
    player_->setVideoFrameCb([this](AVFramePtr frame){
        emit sig_repaint(frame);
    });
    player_->setProcessCb([this](double v){
        emit sig_process(v);
    });
}

void centerWgt::dropEvent(QDropEvent *event)
{
    //获取文件路径(Qstring)
    QList<QUrl>urls = event->mimeData()->urls();
    if (urls.isEmpty()) return;
    //获取文件链表第一个文件路径
    QString qStr = urls.first().toLocalFile();
    QFileInfo fileInfo(qStr);
    //H防止后缀名为大写，例NP4,MKV,,,,便于接下来做判断
    QString fileSuffix = fileInfo.suffix().toLower();//获取并转换为小写0.0
    //判断文件后缀
    if (fileSuffix == "mp4" || fileSuffix == "mkv" || fileSuffix == "flv")
    {
        player_->Open(qStr);
//        QMessageBox::information(this,"提示","打开文件成功");
    }
    else
    {
        QMessageBox::critical(this,"错误","文件格式错误");
    }


}

void centerWgt::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

